import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:flutter/services.dart';

//添加产品弹窗
class AddProductDialog extends StatefulWidget {
  const AddProductDialog({
    required this.callback,
    super.key,
  });
  final Function(Map map) callback; //点击回调
  @override
  State<AddProductDialog> createState() => _AddProductDialogState();
}

class _AddProductDialogState extends State<AddProductDialog> {
  String productName = ''; //产品名称
  int? productQuantity; //产品数量
  //var product; //产品信息

  List productList = []; //产品列表

  @override
  void initState() {
    super.initState();
    _getWarehouseList();
  }

  //获取智慧仓储产品存储量列表
  _getWarehouseList() {
    // myDio
    //     .request(
    //   type: 'get',
    //   url: Interface.wisdomProductList,
    //   queryParameters: {
    //     'pageNum': 1,
    //     'pageSize': 1000,
    //   },
    //   isWisdom: true,
    // )
    //     .then((value) {
    //   if (value['records'] is List) {
    //     productList = value['records'];
    //     setState(() {});
    //   }
    // });
  }

  //获取底部按钮显示
  Widget _getBottomButtonWidget() {
    return Container(
      margin: EdgeInsets.symmetric(horizontal: 12.w),
      height: 40.w,
      decoration: BoxDecoration(
          color: const Color(0xff3074FF), borderRadius: BorderRadius.all(Radius.circular(20.w))),
      alignment: Alignment.center,
      child: Text(
        '确定',
        style: TextStyle(color: Colors.white, fontSize: 16.w, fontWeight: FontWeight.w500),
      ),
    ).clickOnce(() {
      widget.callback({});
      Get.back();
    });
  }

  //获取产品数量显示
  Widget _getProductQuantity() {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      Text(
        '产品数量',
        style:
            TextStyle(color: const Color(0xff000000), fontSize: 14.w, fontWeight: FontWeight.w400),
      ),
      AppGaps.vGap10,
      Container(
          decoration: BoxDecoration(
              color: const Color(0xffFFFFFF),
              border: Border.all(color: const Color(0xff000000).withOpacity(0.1), width: 1.w),
              borderRadius: BorderRadius.all(Radius.circular(4.w))),
          padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 9.w),
          child: TextField(
            keyboardType: TextInputType.number,
            inputFormatters: [
              FilteringTextInputFormatter.digitsOnly, //数字，只能是整数
              LengthLimitingTextInputFormatter(15), //限制长度
            ],
            textInputAction: TextInputAction.done,
            textAlign: TextAlign.start,
            style: TextStyle(
                height: 1.2,
                color: const Color(0xff262F38),
                fontSize: 14.w,
                fontWeight: FontWeight.w400),
            onChanged: (value) {
              if (value != '') {
                productQuantity = int.parse(value);
              }
            },
            decoration: InputDecoration(
              contentPadding: EdgeInsets.only(right: 5.w),
              //防止适配不居中
              isCollapsed: true,
              border: const OutlineInputBorder(
                borderSide: BorderSide.none,
              ),
              hintText: '请输入产品数量',
              hintStyle: TextStyle(
                  height: 1.2,
                  color: const Color(0xffA0A7B5),
                  fontSize: 14.w,
                  fontWeight: FontWeight.w400),
            ),
            maxLines: 1,
            minLines: 1,
          ))
    ]);
  }

  //获取产品名称显示
  Widget _getProductName() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          '产品名称',
          style: TextStyle(
              color: const Color(0xff000000), fontSize: 14.w, fontWeight: FontWeight.w400),
        ),
        SizedBox(
          height: 10.w,
        ),
        Container(
          decoration: BoxDecoration(
              color: const Color(0xffFFFFFF),
              border: Border.all(color: const Color(0xff000000).withOpacity(0.1), width: 1.w),
              borderRadius: BorderRadius.all(Radius.circular(4.w))),
          padding: EdgeInsets.symmetric(horizontal: 8.w, vertical: 9.w),
          child: Row(
            children: [
              Expanded(
                  child: Text(
                productName == '' ? '请选择产品' : productName,
                style: TextStyle(
                    height: 1.2,
                    color: productName == '' ? const Color(0xffA0A7B5) : const Color(0xff262F38),
                    fontSize: 14.w,
                    fontWeight: FontWeight.w400),
              )),
              Icon(
                Icons.expand_more,
                size: 15.w,
                color: const Color(0xffA0A7B5),
              ),
            ],
          ),
        ).clickOnce(() {
          FocusScope.of(context).unfocus();
          // ShowBottomSheetDialog.showDownViewDialog(
          //     context: context,
          //     item: productList,
          //     showValue: 'name',
          //     callback: (value) {
          //       product = value;
          //       productName = value['name'];
          //       setState(() {});
          //     });
        })
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Material(
        type: MaterialType.transparency, //透明类型
        child: Container(
            color: Colors.transparent,
            alignment: Alignment.center,
            child: Container(
              alignment: Alignment.center,
              width: 350.w,
              height: 300.w,
              padding: EdgeInsets.only(top: 15.w),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(20.w),
                gradient: const LinearGradient(
                    //渐变位置
                    begin: Alignment.topCenter,
                    end: Alignment.bottomCenter,
                    //渐变颜色[始点颜色, 结束颜色]
                    colors: [
                      Color(0xffE7EAFF),
                      Color(0xffffffff),
                      Color(0xffffffff),
                      Color(0xffffffff),
                    ]),
              ),
              child: Column(
                children: [
                  Container(
                    padding: EdgeInsets.only(left: 16.w),
                    height: 30.w,
                    width: double.infinity,
                    child: Stack(
                      alignment: Alignment.centerLeft,
                      children: [
                        Text(
                          '添加产品',
                          style: TextStyle(
                            color: const Color(0xff262626),
                            fontSize: 16.sp,
                            fontWeight: FontWeight.w500,
                          ),
                        ),
                        Positioned(
                          top: 0,
                          right: 14.w,
                          child: Container(
                            width: 24.w,
                            height: 24.w,
                            decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.all(Radius.circular(20.w)),
                            ),
                            alignment: Alignment.center,
                            child: Icon(
                              Icons.close,
                              color: const Color(0xffA0A7B5),
                              size: 18.w,
                            ),
                          ).clickOnce(Get.back),
                        ),
                      ],
                    ),
                  ),
                  Container(
                    padding: EdgeInsets.only(top: 10.w, left: 15.w, right: 15.w),
                    child: Column(
                      children: [_getProductName(), AppGaps.vGap16, _getProductQuantity()],
                    ),
                  ),
                  Expanded(
                      child: Container(
                    alignment: Alignment.center,
                    child: _getBottomButtonWidget(),
                  )),
                ],
              ),
            )));
  }
}

//显示添加产品弹窗
showAddProductDialog(Function(Map map) callback) async {
  FocusScope.of(Get.context!).unfocus();
  Get.dialog(AddProductDialog(callback: callback));
}
